import http from '@/plugins/axios'

function getRowspan1(row) {
  let span = 0
  const list1 = row.bodyList || []
  list1.forEach(child1 => {
    const list2 = child1.bodyList || []
    list2.forEach(child2 => {
      const list3 = child2.subAccountList || []
      list3.forEach(() => {
        span ++
      })
    })
  })
  return span
}

function getRowspan2(row) {
  let span = 0
  const list1 = row.bodyList || []
  list1.forEach(child1 => {
    const list2 = child1.subAccountList || []
    list2.forEach(() => {
      span ++
    })
  })
  return span
}


export function getCustomerSubAccountPage(data) {
  return http({
    method: 'POST',
    url: '/ykf/customerSubAccount/page3',
    data
  }).then(res => {
    const l = []
    res.list.forEach((child1, index1) => {
      const list1 = child1.bodyList || []
      const rowspan1 = getRowspan1(child1)
      let colspan1
      list1.forEach((chlid2, index2) => {
        const rowspan2 = getRowspan2(chlid2)
        let colspan2
        const list2 = chlid2.bodyList || []
        list2.forEach((child3, index3) => {
          
          const rowspan3 = child3.subAccountList ? child3.subAccountList.length : 1
          let colspan3
          const list3 = child3.subAccountList || []
          list3.forEach((child4, index4) => {
            l.push({
              ...child1,
              ...chlid2,
              ...child3,
              ...child4,
              _index: index1 + 1,
              index1,
              index2,
              index3,
              index4,
              rowspan1,
              colspan1: colspan1 ? 0 : colspan1 = 1,
              rowspan2,
              colspan2: colspan2 ? 0 : colspan2 = 1,
              rowspan3,
              colspan3: colspan3 ? 0 : colspan3 = 1,
            })
          })
        })
      })
    });
    return { ...res, list: l }
  })
}

export function getShopList() {
  return http({
    method: 'POST',
    url: '/ykf/customerShop/page',
    data: {
      condition: {},
      page: {
        index: 1,
        size: 999999999999
      },
      sort: { order: '', sidx: '' }
    }
  }).then(res => {
    return res.list.map(i => ({ label: i.shopName, value: i.shopId }))
  })
}

export function getShopDistributeRuleList() {
  return http({
    method: 'POST',
    url: '/ykf/shopDistributeRule/page',
    data: {
      condition: {},
      page: {
        index: 1,
        size: 999999999999
      },
      sort: { order: '', sidx: '' }
    }
  }).then(res => {
    return res.list.map(i => ({ label: i.ruleName, value: i.ruleId }))
  })
}

export function getCusSerListList(companyNo) {
  return http({
    method: 'POST',
    url: '/sys/user/cusSerList',
    data: {
      condition: {
        companyNo
      },
      page: {
        index: 1,
        size: 999999999999
      },
      sort: { order: '', sidx: '' }
    }
  }).then(res => {
    return res.list.map(i => ({ label: i.name, value: i.userId }))
  })
}

export function addCustomerSubAccount(data) {
  return http({
    method: 'POST',
    url: '/ykf/customerSubAccount/save',
    data
  })
}

export function updateCustomerSubAccount(data) {
  return http({
    method: 'POST',
    url: '/ykf/customerSubAccount/update',
    data
  })
}

export function updateCustomerSubAccountStatus(data) {
  return http({
    method: 'POST',
    url: '/ykf/customerSubAccount/updateStatus',
    data
  })
}

export function addChildCustomerSubAccount(data) {
  return http({
    method: 'POST',
    url: '/ykf/customerSubAccount/addSubAccount',
    data
  })
}

export function distributeCusSer(data) {
  return http({
    method: 'POST',
    url: '/ykf/customerSubAccount/distributeCusSer',
    data
  })
}

export function getAllExName() {
  return http({
    method: 'POST',
    url: '/ykf/dictDetail/listByNo',
    data: { dictNo: 'company' }
  })
}

export function distributeCompany(data) {
  return http({
    method: 'POST',
    url: '/ykf/customerSubAccount/distributeCompany',
    data
  })
}
